package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.api.domain.StrategyComment;
import cn.wolfcode.wolf2w.business.mapper.StrategyCommentMapper;
import cn.wolfcode.wolf2w.business.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.Date;

/**
 * 攻略评论Service业务层处理
 *
 * @author 沙琪玛
 * @date 2024-11-01
 */
@Service
@Transactional
public class StrategyCommentServiceImpl extends ServiceImpl<StrategyCommentMapper, StrategyComment> implements IStrategyCommentService {

    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @Override
    public IPage<StrategyComment> queryPage(StrategyCommentQuery qo) {
        IPage<StrategyComment> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        LambdaQueryWrapper<StrategyComment> qw = new LambdaQueryWrapper<>();
        qw.eq(StrategyComment::getStrategyId, qo.getStrategyId())
                .orderByDesc(StrategyComment::getCreateTime);
        IPage<StrategyComment> pageResult = baseMapper.selectPage(page, qw);
        for (cn.wolfcode.wolf2w.api.domain.StrategyComment record : pageResult.getRecords()) {
            Long userId = record.getUserId();
            R<UserInfo> inner = remoteUserInfoService.getOne(userId, "inner");
            UserInfo user = inner.getData();
            record.setUser(user);
        }
        return pageResult;
    }

    @Override
    public void add(StrategyComment comment) {
        Long userId = SecurityContextHolder.getUserId();//获取登录用户id//必须在controller里贴@RequiresLogin
        comment.setUserId(userId);
        comment.setCreateTime(new Date());
        comment.setState(1L);
        comment.setThumbnum(0L);
        baseMapper.insert(comment);
    }
}
